class IUP_GRID_BOX
-- Creates a void container for composing elements in a regular grid. It is a 
-- box that arranges the elements it contains from top to bottom and from left 
-- to right, but can distribute the elements in lines or in columns.
--
-- The child elements are added to the control just like a vbox and hbox, 
-- sequentially. Then they are distributed accordingly the attributes 
-- ORIENTATION and NUMDIV. When ORIENTATION=HORIZONTAL children are distributed 
-- from left to right on the first line until NUMDIV, then on the second line, 
-- and so on. When ORIENTATION=VERTICAL children are distributed from top to 
-- bottom on the first column until NUMDIV, then on the second column, and so 
-- on. The number of lines and the number of columns can be easily obtained 
-- from the combination of these attributes.
--
-- Notice that the total number of spaces can be larger than the number of 
-- actual children, the last line or column can be incomplete.
--
-- The column sizes will be based only on the width of the children of the 
-- reference line, usually line 0. The line sizes will be based only on the 
-- height of the children of the reference column, usually column 0.

inherit
	IUP_CONTAINER
	IUP_WIDGET_EXPAND
	IUP_WIDGET_WID
	IUP_WIDGET_SIZE
	IUP_WIDGET_RASTERSIZE
	IUP_WIDGET_USERSIZE
	IUP_WIDGET_FONT
	IUP_WIDGET_CLIENTSIZE
	IUP_WIDGET_CLIENTOFFSET
	IUP_WIDGET_POSITION
	IUP_WIDGET_MAXMIN_SIZE
	IUP_WIDGET_CHILD
	IUP_WIDGET_NAME
	IUP_WIDGET_CUSTOM_ATTRIBUTES

create {ANY}
	grid_box_empty,
   grid_box
			
feature {ANY}
	
	grid_box_empty
	   -- Create an empty grid box.
		local
			p, a_grid_box: POINTER
		do
			a_grid_box := int_grid_box_empty (p)
			set_widget(a_grid_box)
		end
		
	grid_box (col: ARRAY[IUP_WIDGET])
	   -- Create a new grid box containing the list of widgets.
		local
         iterator: ITERATOR[IUP_WIDGET]; i: INTEGER; arg: NATIVE_ARRAY[POINTER]; s: IUP_WIDGET; a_grid_box: POINTER
		do
			i := col.count
			arg := arg.calloc(i)
			iterator := col.new_iterator
			i := 0
			
			from
				iterator.start
         until
				iterator.is_off
         loop
            s := iterator.item
            arg.put(s.widget, i)
				iterator.next
            i := i + 1
         end

			a_grid_box := int_grid_box (arg.to_external)
			set_widget(a_grid_box)
		end

	-- Commands to handle attributes.
	
	set_alignment_line (value: STRING)
		-- Vertically aligns the elements within each line. Possible values: 
		-- "ATOP", "ACENTER", "ABOTTOM". Default: "ATOP".
		-- (non inheritable)
		require
			valid_lin: is_valid_alignment_lin(value)
		do
			iup_open.set_attribute(Current, "ALIGNMENTLIN", value)
		end

	get_alignment_line: STRING
		-- Return the vertical align of the elements within each line.
		do
			Result := iup_open.get_attribute(Current, "ALIGNMENTLIN")
		end

	set_alignment_for_line (value: STRING; line: INTEGER)
	-- Set the alignment of a single line.
		require
			valid_lin: is_valid_alignment_lin(value)
						  line >= 0
		local
			str: STRING
		do
			str := "ALIGNMENTLIN" + line.to_string
			iup_open.set_attribute(Current, str, value)
		end

	get_alignment_for_line (line: INTEGER): STRING
		-- Return the alignment used at the line.
		require
			line >= 0
		local
			str: STRING
		do
			str := "ALIGNMENTLIN" + line.to_string
			Result := iup_open.get_attribute(Current, str)
		end
	
	set_alignment_column (value: STRING)
		-- Horizontally aligns the elements within each column. Possible values: 
		-- "ALEFT", "ACENTER", "ARIGHT". Default: "ALEFT".
		-- (non inheritable)
		require
			valid_col: is_valid_alignment_col(value)
		do
			iup_open.set_attribute(Current, "ALIGNMENTCOL", value)
		end

	get_alignment_column: STRING
		-- Return the horizontal align of the elements within each line.
		do
			Result := iup_open.get_attribute(Current, "ALIGNMENTCOL")
		end

	set_alignment_for_column (value: STRING; col: INTEGER)
	-- Set the alignment of a single column.
		require
			valid_col: is_valid_alignment_col(value)
						  col >= 0
		local
			str: STRING
		do
			str := "ALIGNMENTCOL" + col.to_string
			iup_open.set_attribute(Current, str, value)
		end

	get_alignment_for_column (col: INTEGER): STRING
		-- Return the alignment used at the column.
		require
			col >= 0
		local
			str: STRING
		do
			str := "ALIGNMENTCOL" + col.to_string
			Result := iup_open.get_attribute(Current, str)
		end

	set_expand_children (value: STRING)
		-- Forces all children to expand in the given direction and to fully 
		-- occupy its space available inside the box. Can be YES (both 
		-- directions), HORIZONTAL, VERTICAL or NO. Default: "NO". This has the 
		-- same effect as setting EXPAND on each child.
		-- (non inheritable).
		require
			valid_expand_children: is_valid_expand_children(value)
		do
			iup_open.set_attribute(Current, "EXPANDCHILDREN", value)
		end

	get_expand_children: STRING
		-- Return the value of the used expand children.
		do
			Result := iup_open.get_attribute(Current, "EXPANDCHILDREN")
		end

	set_fit_to_children_at_line (line: INTEGER)
		-- (write-only) Set the RASTERSIZE attribute of the reference element in 
		-- the given line, so that it will fit the highest element in the line.  
		-- Can only be set after the layout of the dialog has been calculated at 
		-- least 1 time. If FITMAXWIDTHn or FITMAXHEIGHTn are set for the line 
		-- they are used as maximum limit for the size.
		require
			line > 0
		local
			str: STRING
		do
			str := "L" + line.to_string
			iup_open.set_attribute(Current, "FITTOCHILDREN", str)
		end

	set_fit_to_children_at_column (column: INTEGER)
		-- (write-only) Set the RASTERSIZE attribute of the reference element in 
		-- the given column, so that it will fit the largest element in the 
		-- column. Can only be set after the layout of the dialog has been 
		-- calculated at least 1 time. If FITMAXWIDTHn or FITMAXHEIGHTn are set 
		-- for the column they are used as maximum limit for the size.
		require
			column > 0
		local
			str: STRING
		do
			str := "C" + column.to_string
			iup_open.set_attribute(Current, "FITTOCHILDREN", str)
		end

	set_gaplin (space: INTEGER)
		-- Defines a vertical space in pixels between lines. Default: "0".
		require
			space >= 0
		do
			iup_open.set_attribute(Current, "GAPLIN", space.to_string)
		end

	get_gaplin: INTEGER
		-- Return the vertical space between lines.
		local
			value: STRING
		do
			value := iup_open.get_attribute(Current, "GAPLIN")
			Result := value.to_integer
		end

	set_cgaplin (space: INTEGER)
		-- Defines a vertical space between lines in the same units of the SIZE 
		-- attribute for the height. Default: "0".
		require
			space >= 0
		do
			iup_open.set_attribute(Current, "CGAPLIN", space.to_string)
		end

	get_cgaplin: INTEGER
		-- Return the value of the attribute cgaplin.
		local
			value: STRING
		do
			value := iup_open.get_attribute(Current, "CGAPLIN")
			Result := value.to_integer
		end

	set_gapcol (space: INTEGER)
		-- Defines an horizontal space in pixels between columns. Default: "0".
		require
			space >= 0
		do
			iup_open.set_attribute(Current, "GAPCOL", space.to_string)
		end

	get_gapcol: INTEGER
		-- Return the horizontal space between columns.
		local
			value: STRING
		do
			value := iup_open.get_attribute(Current, "GAPCOL")
			Result := value.to_integer
		end

	set_cgapcol (space: INTEGER)
		-- Defines an horizontal space between columns in the same units of the 
		-- SIZE attribute for the height. Default: "0".
		require
			space >= 0
		do
			iup_open.set_attribute(Current, "CGAPCOL", space.to_string)
		end

	get_cgapcol: INTEGER
		-- Return the value of the attribute cgapcol.
		local
			value: STRING
		do
			value := iup_open.get_attribute(Current, "CGAPCOL")
			Result := value.to_integer
		end

	set_ngaplin (space: INTEGER)
		-- Same as *GAPLIN* but non inheritable.
		require
			space >= 0
		do
			iup_open.set_attribute(Current, "NGAPLIN", space.to_string)
		end

	get_ngaplin: INTEGER
		-- Return the value of *NGAPLIN* attribute.
		local
			value: STRING
		do
			value := iup_open.get_attribute(Current, "NGAPLIN")
			Result := value.to_integer
		end

	set_ncgaplin (space: INTEGER)
		-- Same as *CGAPLIN* but non inheritable.
		require
			space >= 0
		do
			iup_open.set_attribute(Current, "NCGAPLIN", space.to_string)
		end

	get_ncgaplin: INTEGER
		-- Return the value of *NCGAPLIN* attribute.
		local
			value: STRING
		do
			value := iup_open.get_attribute(Current, "NCGAPLIN")
			Result := value.to_integer
		end
	
	set_ngapcol (space: INTEGER)
		-- Same as *GAPCOL* but non inheritable.
		require
			space >= 0
		do
			iup_open.set_attribute(Current, "NGAPCOL", space.to_string)
		end

	get_ngapcol: INTEGER
		-- Return the value of *NGAPCOL* attribute.
		local
			value: STRING
		do
			value := iup_open.get_attribute(Current, "NGAPCOL")
			Result := value.to_integer
		end

	set_ncgapcol (space: INTEGER)
		-- Same as *CGAPCOL* but non inheritable.
		require
			space >= 0
		do
			iup_open.set_attribute(Current, "NCGAPCOL", space.to_string)
		end

	get_ncgapcol: INTEGER
		-- Return the value of *NCGAPCOL* attribute.
		local
			value: STRING
		do
			value := iup_open.get_attribute(Current, "NCGAPCOL")
			Result := value.to_integer
		end

	set_homogeneous_lines (state: BOOLEAN)
		-- (non inheritable): forces all lines to have the same vertical space, 
		-- or height. The line height will be  based on the highest child of the 
		-- reference column (See set_sizecol). Default: "NO". Notice that this 
		-- does not changes the children size, only the available space for each 
		-- one of them to expand.
		do
			iup_open.set_attribute(Current, "HOMOGENEOUSLIN", boolean_to_yesno(state))
		end

	is_homogeneous_lines: BOOLEAN
		-- Return the state of homogeneouslin.
		local
			str: STRING
		do
			str := iup_open.get_attribute(Current, "HOMOGENEOUSLIN")
			Result := yesno_to_boolean(str)
		end


	set_homogeneous_columns (state: BOOLEAN)
		-- (non inheritable): forces all columns to have the same horizontal 
		-- space, or width. The column width will be  based on the largest child 
		-- of the reference line (See set_sizelin). Default: "NO". Notice that 
		-- this does not changes the children size, only the available space for 
		-- each one of them to expand.
		do
			iup_open.set_attribute(Current, "HOMOGENEOUSCOL", boolean_to_yesno(state))
		end

	is_homogeneous_columns: BOOLEAN
		-- Return the state of homogeneouscol.
		local
			str: STRING
		do
			str := iup_open.get_attribute(Current, "HOMOGENEOUSCOL")
			Result := yesno_to_boolean(str)
		end

	set_margin (horizontal, vertical: INTEGER)
		-- Defines a margin in pixels. Default: "0x0" (no margin).
		require
			horizontal >= 0
			vertical >= 0
		local
			margin: STRING
		do
			margin := horizontal.to_string
			margin.append_string("x")
			margin.append_string(vertical.to_string)

			iup_open.set_attribute(Current, "MARGIN", margin)
		end

	get_margin: TUPLE[INTEGER, INTEGER]
		-- Return the value of the margins.
		local
			margin: STRING
		do
			margin := iup_open.get_attribute(Current, "MARGIN")
			Result := components_of_size(margin)
		end

	set_cmargin (horizontal, vertical: INTEGER)
		-- Defines a margin in the same units of the SIZE attribute. 
		-- Default:	"0x0" (no margin).
		require
			horizontal >= 0
			vertical >= 0
		local
			margin: STRING
		do
			margin := horizontal.to_string
			margin.append_string("x")
			margin.append_string(vertical.to_string)

			iup_open.set_attribute(Current, "CMARGIN", margin)
		end

	get_cmargin: TUPLE[INTEGER, INTEGER]
		-- Return the value of the CMARGIN.
		local
			margin: STRING
		do
			margin := iup_open.get_attribute(Current, "CMARGIN")
			Result := components_of_size(margin)
		end

	set_nmargin (horizontal, vertical: INTEGER)
		-- (non inheritable): Same as MARGIN but are non inheritable.
		require
			horizontal >= 0
			vertical >= 0
		local
			margin: STRING
		do
			margin := horizontal.to_string
			margin.append_string("x")
			margin.append_string(vertical.to_string)

			iup_open.set_attribute(Current, "NMARGIN", margin)
		end

	get_nmargin: TUPLE[INTEGER, INTEGER]
		-- Return the value of the nmargin.
		local
			margin: STRING
		do
			margin := iup_open.get_attribute(Current, "NMARGIN")
			Result := components_of_size(margin)
		end

	set_ncmargin (horizontal, vertical: INTEGER)
		-- (non inheritable): Same as CMARGIN but are non inheritable.
		require
			horizontal >= 0
			vertical >= 0
		local
			margin: STRING
		do
			margin := horizontal.to_string
			margin.append_string("x")
			margin.append_string(vertical.to_string)

			iup_open.set_attribute(Current, "NCMARGIN", margin)
		end

	get_ncmargin: TUPLE[INTEGER, INTEGER]
		-- Return the value of NCMARGIN.
		local
			margin: STRING
		do
			margin := iup_open.get_attribute(Current, "NCMARGIN")
			Result := components_of_size(margin)
		end

	set_numdiv (num: INTEGER)
		-- Controls the number of divisions along the distribution according to 
		-- ORIENTATION. When ORIENTATION=HORIZONTAL, NUMDIV is the number of 
		-- columns, when ORIENTATION=VERTICAL, NUMDIV is the number of lines. 
		-- When value is 0, its actual value will be calculated to fit the 
		-- maximum number of elements in the orientation direction. Default: 0.
		require
			num >= 0
		local
			str: STRING
		do
			if num.is_equal(0) then
				str := "AUTO"
			else
				str := num.to_string
			end

			iup_open.set_attribute(Current, "NUMDIV", str)		
		end

	get_numdiv: INTEGER
		-- Return the number of divisions.
		local
			str: STRING
		do
			str := iup_open.get_attribute(Current, "NUMDIV")

			if str.is_equal("AUTO") then
				Result := 0
			else 
				Result := str.to_integer
			end
		end

	get_lines: INTEGER
		-- (read-only): returns the number of lines.
		local
			str: STRING
		do
			str := iup_open.get_attribute(Current, "NUMLIN")
			Result := str.to_integer
		end

	get_columns: INTEGER
		-- (read-only): returns the number of columns.
		local
			str: STRING
		do
			str := iup_open.get_attribute(Current, "NUMCOL")
			Result := str.to_integer
		end

	set_normalize_size (value: STRING)
		-- (non inheritable): normalizes all children natural size to be the 
		-- biggest natural size among the reference line and/or the reference 
		-- column. All natural width will be set to the biggest width, and all 
		-- natural height will be set to the biggest height according to is 
		-- value. Can be NO, HORIZONTAL (width only), VERTICAL (height only) or 
		-- BOTH. Default: "NO". Same as using IUP_NORMALIZER. Notice that this is 
		-- different from the HOMOGENEOUS* attributes in the sense that the 
		-- children will have their sizes changed.
		require
			is_valid_normalize_size(value)
		do
			iup_open.set_attribute(Current, "NORMALIZESIZE", value)
		end

	get_normalize_size: STRING
		-- Return the value of normalize size.
		do
			Result := iup_open.get_attribute(Current, "NORMALIZESIZE")
		end

	set_horizontal_orientation
		-- (non inheritable): controls the distribution of the children in 
		-- columns. This is the default value. 
		do
			iup_open.set_attribute(Current, "ORIENTATION", "HORIZONTAL")
		end

	set_vertical_orientation
		-- (non inheritable): controls the distribution of the children in 
		-- lines.
		do
			iup_open.set_attribute(Current, "ORIENTATION", "VERTICAL")
		end

	is_horizontal: BOOLEAN
		local
			str: STRING
		do
			str := iup_open.get_attribute(Current, "ORIENTATION")

			if str.is_equal("HORIZONTAL") then
				Result := True
			else
				Result := False
			end
		end

	is_vertical: BOOLEAN
		local
			str: STRING
		do
			str := iup_open.get_attribute(Current, "ORIENTATION")

			if str.is_equal("VERTICAL") then
				Result := True
			else
				Result := False
			end
		end

	set_reference_column (col: INTEGER)
		-- (non inheritable): index of the column that will be used as reference 
		-- when calculating the height of the lines. Default: 0.
		require
			col >= 0
		do
			iup_open.set_attribute(Current, "SIZECOL", col.to_string)
		end

	get_reference_column: INTEGER
		-- Return the index of the column used as reference.
		local
			str: STRING
		do
			str := iup_open.get_attribute(Current, "SIZECOL")
			Result := str.to_integer
		end

	set_reference_line (line: INTEGER)
		-- (non inheritable): index of the line that will be used as reference 
		-- when calculating the width of the columns. Default: 0.
		require
			line >= 0
		do
			iup_open.set_attribute(Current, "SIZELIN", line.to_string)
		end

	get_reference_line: INTEGER
		-- Return the index of the line used as reference.
		local
			str: STRING
		do
			str := iup_open.get_attribute(Current, "SIZELIN")
			Result := str.to_integer
		end
						
feature {}

	-- Internals
	
	int_grid_box_empty (arguments: POINTER): POINTER
		external "plug_in"
      alias "{
         location: "${sys}/plugins"
         module_name: "iup"
         feature_name: "IupGridBox"
         }"
      end
	
   int_grid_box (arguments: POINTER): POINTER
		external "plug_in"
      alias "{
         location: "${sys}/plugins"
         module_name: "iup"
         feature_name: "IupGridBoxv"
         }"
      end

	-- Validations

	is_valid_alignment_lin (value: STRING): BOOLEAN
		do
			if value.is_equal("ATOP") or
				value.is_equal("ACENTER") or
				value.is_equal("ABOTTOM") then
				Result := True
			else
				Result := False
			end
		end

	is_valid_alignment_col (value: STRING): BOOLEAN
		do
			if value.is_equal("ALEFT") or
				value.is_equal("ACENTER") or
				value.is_equal("ARIGHT") then
				Result := True
			else
				Result := False
			end
		end

	is_valid_expand_children (value: STRING): BOOLEAN
		do
			if value.is_equal("YES") or
				value.is_equal("HORIZONTAL") or
				value.is_equal("VERTICAL") or
				value.is_equal("NO") then
				Result := True
			else
				Result := False
			end
		end

	is_valid_normalize_size (value: STRING): BOOLEAN
		do
			if value.is_equal("BOTH") or
				value.is_equal("HORIZONTAL") or
				value.is_equal("VERTICAL") or
				value.is_equal("NO") then
				Result := True
			else
				Result := False
			end
		end
	
end -- class IUP_GRID_BOX

-- The MIT License (MIT)

-- Copyright (c) 2016, 2017 by German A. Arias

-- Permission is hereby granted, free of charge, to any person obtaining a copy
-- of this software and associated documentation files (the "Software"), to deal
-- in the Software without restriction, including without limitation the rights
-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-- copies of the Software, and to permit persons to whom the Software is
-- furnished to do so, subject to the following conditions:
--
-- The above copyright notice and this permission notice shall be included in 
-- all copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-- SOFTWARE.
